package com.hnao.serialport.utils.sqym;


public class CardNoTransUtil {
    public CardNoTransUtil() {
    }

    public static String transHex2Byte2U8Str(String hex) {
        return new String(hexToByte(hex));
    }

    public static String trans8H10D(String hex) {
        return trans8H10D(hex, true);
    }

    public static String trans8H10D(String hex, boolean prosFlag) {
        if (null == hex) {
            return null;
        } else if (hex.length() == 8 && isHexNumberRex(hex)) {
            if (!prosFlag) {
                hex = reversalHex(hex);
            }

            return String.format("%010d", Long.parseLong(hex, 16));
        } else {
            throw new RuntimeException("卡值格式不正确");
        }
    }

    public static String trans10D8H(String octNumber) {
        return trans10D8H(octNumber, true);
    }

    public static String trans10D8H(String octNumber, boolean prosFlag) {
        if (null == octNumber) {
            return null;
        } else if (octNumber.length() <= 10 && isOctNumberRex(octNumber)) {
            octNumber = String.format("%010d", Long.parseLong(octNumber));
            String hex = Long.toHexString(Long.parseLong(octNumber));
            if (!prosFlag) {
                hex = reversalHex(hex);
            }

            return hex;
        } else {
            throw new RuntimeException("卡值格式不正确");
        }
    }

    private static String reversalHex(String hex) {
        StringBuilder tempData = new StringBuilder();
        int cardLength = hex.length() / 2;

        for(int i = 0; i < cardLength; ++i) {
            tempData.append(hex.substring((cardLength - i - 1) * 2, (cardLength - i) * 2));
        }

        hex = tempData.toString();
        return hex;
    }

    private static boolean isOctNumber(String str) {
        boolean flag = false;
        int i = 0;

        for(int n = str.length(); i < n; ++i) {
            char c = str.charAt(i);
            if (c == '0' | c == '1' | c == '2' | c == '3' | c == '4' | c == '5' | c == '6' | c == '7' | c == '8' | c == '9') {
                flag = true;
            }
        }

        return flag;
    }

    private static boolean isHexNumber(String str) {
        boolean flag = false;

        for(int i = 0; i < str.length(); ++i) {
            char cc = str.charAt(i);
            if (cc == '0' || cc == '1' || cc == '2' || cc == '3' || cc == '4' || cc == '5' || cc == '6' || cc == '7' || cc == '8' || cc == '9' || cc == 'A' || cc == 'B' || cc == 'C' || cc == 'D' || cc == 'E' || cc == 'F' || cc == 'a' || cc == 'b' || cc == 'c' || cc == 'c' || cc == 'd' || cc == 'e' || cc == 'f') {
                flag = true;
            }
        }

        return flag;
    }

    private static boolean isOctNumberRex(String str) {
        String validate = "\\d+";
        return str.matches(validate);
    }

    private static boolean isHexNumberRex(String str) {
        String validate = "(?i)[0-9a-f]+";
        return str.matches(validate);
    }

    private static byte[] hexToByte(String hex) {
        int byteLen = hex.length() / 2;
        byte[] ret = new byte[byteLen];

        for(int i = 0; i < byteLen; ++i) {
            int m = i * 2 + 1;
            int n = m + 1;
            int intVal = Integer.decode("0x" + hex.substring(i * 2, m) + hex.substring(m, n));
            ret[i] = Byte.valueOf((byte)intVal);
        }

        return ret;
    }
}
